//
//  YSGToast.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2022/12/21.
//  Copyright © 2022 QDSG. All rights reserved.
//

import UIKit
import Toast_Swift

enum YSGToastEnum {
    case warring
    case success
}

class YSGToast {
    class func showToast(text: String?, duration: TimeInterval = ToastManager.shared.duration, type: YSGToastEnum) {
        switch type {
        case .warring:
            showToast(warring: text, duration: duration)
        case .success:
            break
        }
    }
    
    private class func showToast(warring: String?, duration: TimeInterval = ToastManager.shared.duration) {
        UIApplication.keyWindows()?.showToast(YSGToastView(image: UIImage(named: "common_alert_warring"), text: warring), point: YYTextCGRectGetCenter(UIScreen.main.bounds))
    }
}

private class YSGToastView: UIView {
    init(image: UIImage?, text: String?) {
        super.init(frame: UIScreen.main.bounds)
        backgroundColor = .black.withAlphaComponent(0.3)
        
        let view = UIView()
        addSubview(view)
        view.snp.makeConstraints { make in
            make.centerX.centerY.equalToSuperview()
        }
        
        view.backgroundColor = .white
        view.layer.cornerRadius = 14
        view.layer.masksToBounds = true
        
        let icon = UIImageView(image: image)
        view.addSubview(icon)
        icon.snp.makeConstraints { make in
            make.top.equalTo(24)
            make.centerX.equalToSuperview()
        }
        
        let title = UILabel()
        view.addSubview(title)
        title.snp.makeConstraints { make in
            make.top.equalTo(icon.snp.bottom).offset(12)
            make.left.equalTo(23)
            make.right.equalTo(-23)
            make.width.greaterThanOrEqualTo(100)
            make.bottom.equalTo(-24)
        }
        title.text = text
        title.font = .regular_14
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
